package win.ganbo.softwareFive.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import win.ganbo.softwareFive.domain.LoginToken;
import win.ganbo.softwareFive.domain.User;
import win.ganbo.softwareFive.service.IUserSeervice;
import win.ganbo.softwareFive.utils.DateUtil;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserSeervice userService;

    @RequestMapping("/listUI")
    public String listUI(HttpServletRequest request) {
        List<?> list = userService.findByHql("FROM User o ORDER BY id desc");
        request.setAttribute("users", list);
        return "list";
    }

    @RequestMapping("/submit")
    public String submit(User user, HttpSession session, HttpServletRequest request) {
        LoginToken loginToken = getLoginUser(session);
        if (loginToken == null) {
            return "redirect:/user/listUI";
        }

        user.setNumber(Long.valueOf(loginToken.getUsername()));//获取学号
        user.setIp(request.getRemoteHost());
        user.setSex("男");
        //        user.setClassAndMajor("软件工程五班");

        //yyyy_MM_dd
        user.setInSchoolTime(DateUtil.parseDate("2011_09_00"));
        user.setOutSchoolTime(DateUtil.parseDate("2015_06_00"));
        userService.save(user);
        return "redirect:listUI";
    }
}
